package org.celery.modules.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.goods.entity.GoodsCategory;
import org.celery.modules.goods.vo.GoodsCategoryVO;
import org.celery.modules.goods.wrapper.GoodsCategoryWrapper;
import org.celery.modules.goods.service.IGoodsCategoryService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品类别表. 控制器
 *
 * @author luoqc
 * @since 2021-10-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/celery-goods/goods-category")
@Api(value = "商品类别表.", tags = "商品类别表.接口")
public class GoodsCategoryController extends BladeController {

	private final IGoodsCategoryService goodsCategoryService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入goodsCategory")
	public R<GoodsCategoryVO> detail(GoodsCategory goodsCategory) {
		GoodsCategory detail = goodsCategoryService.getOne(Condition.getQueryWrapper(goodsCategory));
		return R.data(GoodsCategoryWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商品类别表.
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsCategory")
	public R<IPage<GoodsCategoryVO>> list(GoodsCategory goodsCategory, Query query, BladeUser bladeUser) {
		QueryWrapper<GoodsCategory> queryWrapper = Condition.getQueryWrapper(goodsCategory);
		if (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) {
			queryWrapper.lambda().in(GoodsCategory::getTenantId, new ArrayList<String>() {{
				add(BladeConstant.ADMIN_TENANT_ID);
				add(bladeUser.getTenantId());
			}});
		}
		queryWrapper.lambda().orderByAsc(GoodsCategory::getTenantId);
		queryWrapper.lambda().orderByAsc(GoodsCategory::getSort);
		IPage<GoodsCategory> pages = goodsCategoryService.page(Condition.getPage(query), queryWrapper);
		return R.data(GoodsCategoryWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商品类别表.
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入goodsCategory")
	public R<IPage<GoodsCategoryVO>> page(GoodsCategoryVO goodsCategory, Query query) {
		IPage<GoodsCategoryVO> pages = goodsCategoryService.selectGoodsCategoryPage(Condition.getPage(query), goodsCategory);
		return R.data(pages);
	}

	/**
	 * 新增 商品类别表.
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入goodsCategory")
	public R save(@Valid @RequestBody GoodsCategory goodsCategory, BladeUser bladeUser) {
		goodsCategory.setTenantId(bladeUser.getTenantId());
		return R.status(goodsCategoryService.save(goodsCategory));
	}

	/**
	 * 修改 商品类别表.
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入goodsCategory")
	public R update(@Valid @RequestBody GoodsCategory goodsCategory) {
		return R.status(goodsCategoryService.updateById(goodsCategory));
	}

	/**
	 * 新增或修改 商品类别表.
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入goodsCategory")
	public R submit(@Valid @RequestBody GoodsCategory goodsCategory, BladeUser bladeUser) {
		if (Func.isEmpty(goodsCategory.getTenantId())) {
			goodsCategory.setTenantId(bladeUser.getTenantId());
		}
		if (!goodsCategory.getTenantId().equals(bladeUser.getTenantId())) {
			throw new ServiceException("没有修改权限");
		}
		return R.status(goodsCategoryService.saveOrUpdate(goodsCategory));
	}

	
	/**
	 * 删除 商品类别表.
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids, BladeUser bladeUser) {
		List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(Wrappers.<GoodsCategory>lambdaQuery()
				.in(GoodsCategory::getId, ids)
		);
		for (GoodsCategory goodsCategory : goodsCategoryList) {
			if (!goodsCategory.getTenantId().equals(bladeUser.getTenantId())) {
				throw new ServiceException("没有修改权限");
			}
		}
		return R.status(goodsCategoryService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 选项 商品类别表.
	 */
	@GetMapping("/select")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsCategory")
	public R<List<GoodsCategory>> select(GoodsCategory goodsCategory, BladeUser bladeUser) {
		QueryWrapper<GoodsCategory> queryWrapper = Condition.getQueryWrapper(goodsCategory);
		if (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) {
			queryWrapper.lambda().in(GoodsCategory::getTenantId, new ArrayList<String>() {{
				add(BladeConstant.ADMIN_TENANT_ID);
				add(bladeUser.getTenantId());
			}});
		}
		queryWrapper.lambda().orderByAsc(GoodsCategory::getTenantId);
		queryWrapper.lambda().orderByAsc(GoodsCategory::getSort);
		return R.data(goodsCategoryService.list(queryWrapper));
	}
	
}
